﻿/*
=============================================================================
Author:魏宝辉通过CodeSmithe生成
CreatedTime:2012-10-14
Description:usergroup.aspx.cs页面代码
=============================================================================	
*/

using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using ExtAspNet;
using FTchina.SearchQuery;
using FTchina.Library;
using FTchina;
 
public partial class Usergroup : System.Web.UI.Page 
{

    string TableName = "usergroup";//表名
    static string SqlWhere = " 1=1 ";//过滤条件
    string GetCountSql = "select count(id)  from usergroup where " + SqlWhere ;
    string SqlTable = "select *  from usergroup where " + SqlWhere + "  order by id desc ";
    string ReturnPage = "usergroup.aspx";
    string SearchFilter = " and ( id like '%{0}%'  )";//默认搜索id，其他字段按需添加

    FTchina.SearchQuery.QueryModel qm = new FTchina.SearchQuery.QueryModel();//实例化模型
    string WindowsPage = "usergroupaction.aspx";//弹出窗口的页面地址

    protected void Page_Load(object sender, EventArgs e)
    {
        FTchina.basic.CheckAdmin(this);        
        FTchina.basic.ChkPoewr(this, FTchina.Api.htmlpath.GetCurrentPagePath());
 		if (!IsPostBack)
        {	
			btnDelete.OnClientClick = Grid1.GetNoSelectionAlertReference("至少选择一项！");
            btnNew.OnClientClick = Window1.GetShowReference(WindowsPage, "新增"); 
            if (qm.WorkPage != ReturnPage)
            {
                qm.TableName = TableName;
                qm.GetCountSql = GetCountSql;
                qm.GetTableSql = SqlTable;
                qm.ReturnPage = ReturnPage;
                qm.SqlWhere = SqlWhere;
                qm.WorkPage = ReturnPage;
                qm.Key1 = string.Empty;
            }
            BindGrid(qm);
		}
    }
	
	 
    #region 绑定列表
    /// <summary>
    /// 绑定表格
    /// </summary>
    private void BindGrid(QueryModel qm)
    {
        //每页条数
        Grid1.PageSize = 20;
        //总数
        Grid1.RecordCount = Convert.ToInt32(FTchina.shujuku.GetDataTable(qm.GetCountSql).Rows[0][0].ToString());//数据总条目数
        //分页数据源
        Grid1.DataSource = FTchina.shujuku.ShowForPage(qm.TableName, qm.SqlWhere, Grid1.PageIndex, Grid1.PageSize,"desc");
        Grid1.DataBind();

        if (qm.Key1.Length > 0)
        {
            ttbSearch.Text = qm.Key1;
            ttbSearch.ShowTrigger1 = true;
        }

    }
    /// <summary>
    /// 分页事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Grid1_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
    {

        Grid1.PageIndex = e.NewPageIndex;
        BindGrid(qm);
    }
    /// <summary>
    /// 删除按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        StringBuilder sb = new StringBuilder();
        foreach (int row in Grid1.SelectedRowIndexArray)
        {
            sb.Append(Grid1.DataKeys[row][0].ToString());
            sb.Append(",");
        }
        foreach (string ids in sb.ToString().TrimEnd(',').Split(','))
        {
            FTchina.BLL.UsergroupManager.DeleteUsergroupByid(Convert.ToInt32(ids));
        }
        BindGrid(qm);
        Grid1.SelectedRowIndexArray=new int[]{};//去除默认的选项
    }

     
    /// <summary>
    /// 弹出窗口返回的处理
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Window1_Close(object sender, EventArgs e)
    {
        BindGrid(qm);
    }
    #endregion

    #region 搜索过滤
    /// <summary>
    /// 点击搜索上的关闭按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ttbSearch_Trigger1Click(object sender, EventArgs e)
    {
        ttbSearch.ShowTrigger1 = false;
        ttbSearch.Text = String.Empty;//清空搜索字符串
        qm.Key1 = String.Empty;//清空搜索条件
        qm.GetCountSql = GetCountSql;
        qm.SqlWhere = SqlWhere;
        BindGrid(qm);
    }
    /// <summary>
    /// 点击搜索按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ttbSearch_Trigger2Click(object sender, EventArgs e)
    {
        ttbSearch.ShowTrigger1 = true;//显示关闭搜索按钮
        string SqlWhere = " 1=1  ";//过滤条件，清空其他的过滤条件
        if (ttbSearch.Text.Trim().Length > 0)
        {
            qm.Key1 = ttbSearch.Text.Trim();
            SqlWhere += string.Format(SearchFilter, RequestHelper.SafeRequest(qm.Key1));
        }
        else
        {
            qm.Key1 = "";
        }
 		 string GetCountSql = "select count(id)  from usergroup where " + SqlWhere ;
         qm.GetCountSql = GetCountSql;//总数
        qm.SqlWhere = SqlWhere;

        BindGrid(qm);
    }
   

    #endregion


    #region 导出Excel
    /// <summary>
    /// 带过滤条件的导出，不分页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnExcel_click(object sender, EventArgs e)
    {
        string sql = "select * from {0} where {1}";
        DataSet ds = shujuku.GetDataSet(string.Format(sql, qm.TableName, qm.SqlWhere));
        btnExcel.Enabled = true;
        FTchina.OfficeHelper.ToExcel.DataSetToExcel(ds);
        
    }
    #endregion

    
}




 

